//
//  CXUserInfo.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/21.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

class CXUserInfo: NSObject, NSCoding {

///  用户信息JSon格式从新浪的API文档获得
    
    /// 用户的在新浪微博的 Id
    var uid: String?
    /// 过期时间,单位是 `秒`
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expireDate = NSDate().dateByAddingTimeInterval(expires_in)
        }
    }//基础类型使用KVC前必须初始化
    /// access_token
    var access_token: String?
    /// 过期时间的字符串类型
    var remind_in: String?
    /// 用户的大图 180*180
    var avatar_large: String?
    /// 用户的昵称
    var screen_name: String?
    /// 自定义属性 -- 过期日期
    var expireDate: NSDate?
    
    init(dict: [String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        //重写getter
        let keys = ["uid","expires_in", "access_token", "avatar_large", "screen_name"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
///  归档解档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expireDate, forKey: "expireDate")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    required init?(coder aDecoder: NSCoder) {
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expireDate = aDecoder.decodeObjectForKey("expireDate") as? NSDate
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
}
